<?php

namespace Apps\AppApi\Method\Question;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\QuestionAnswer;
use Apps\Common\Models\QuestionAnswerFollow;
use Apps\Common\Models\UserAttribute;
use Apps\Common\Models\UserBase;
use Apps\Common\Models\QuestionAnswerComments;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Libs\Jpush;

/**
 * 问题的专家回复列表
 * @package Apps\AppApi\Method\Question
 */
class AnswerExpertList extends BaseMethod
{
    public function run()
    {
        $questionId = intval($this->getDataItem('question_id', 0));
        $user_id = intval($this->getDataItem('user_id', 0));
        $page = intval($this->getDataItem('page', 1));

//        $questionId = 194;

        if ($questionId < 1) {
            return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '参数不完整或者参数错误！');
        }

        $limit = $this->getConfig()->limit;
        $offset = ($page-1) * $limit;

        $QuestionAnswer = new QuestionAnswer();

        $expertReplyList = $QuestionAnswer->query()->columns([
            'u.user_id',
            'u.user_cover',
            'u.user_nickname',
            'answer_id',
            'answer_content',
            'support_count',
            'answer_addtime',
            'answer_pics',
        ])->leftJoin('\Apps\Common\Models\UserBase', 'u.user_id = \Apps\Common\Models\QuestionAnswer.user_id', 'u')->where("answer_state = 1 and type = 2 and question_id = {$questionId}")->orderBy('answer_addtime DESC')->limit($limit, $offset)->execute()->toArray();

        $QuestionAnswerComments = new QuestionAnswerComments();
        $UserBase = new UserBase();
        $QuestionAnswerFollow = new QuestionAnswerFollow();
        foreach ($expertReplyList as $k => $val) {
            $UserAttribute = UserAttribute::query()->columns(['attr_value_json'])->where('attr_type = 103 and attr_state = 1 and user_id = ' . $val['user_id'])->execute()->getFirst();

            if ($UserAttribute && $UserAttribute->attr_value_json) {
                $objAttrValueJoin = json_decode($UserAttribute->attr_value_json);
                $expertReplyList[$k]['experttitle'] = $objAttrValueJoin->experttitle;
                $expertReplyList[$k]['field'] = $objAttrValueJoin->field ?: [];
            } else {
                $expertReplyList[$k]['experttitle'] = '';
                $expertReplyList[$k]['field'] = [];
            }
            $expertReplyList[$k]['user_cover'] = PicUrl::UserCover($val['user_cover'], $this->getDi());
            $expertReplyList[$k]['answer_content'] = unserialize(base64_decode($val['answer_content'])) ?: $val['answer_content'];
            if ($user_id > 0) {
                $expertReplyList[$k]['is_support'] = $QuestionAnswerFollow->is_support($user_id, $val['answer_id']) ? '1' : '0';
            } else {
                $expertReplyList[$k]['is_support'] = '0';
            }

            $answer_pics = [];
            if ($answer_pics_list = json_decode($val['answer_pics'], true)) {
                foreach ($answer_pics_list as $value) {
                    $answer_pics[] = PicUrl::Question($value, $this->getDi());
                }
            }
            $expertReplyList[$k]['answer_pics'] = $answer_pics;
            unset($expertReplyList[$k]['attr_value_json']);
            if ($item = $QuestionAnswerComments->query()->where('answer_id = ' . $val['answer_id'])->limit(10, 0)->execute()->toArray()) {
                foreach ($item as $key => $v) {
                    $user_info = $UserBase->getOne($v['user_id']);
                    $to_user_info = $UserBase->getOne($v['to_user_id']);
                    $item[$key]['user_info'] = [
                        'user_id' => $v['user_id'],
                        'user_nickname' => $user_info['user_nickname']
                    ];
                    $item[$key]['to_user_info'] = [
                        'user_id' => $v['to_user_id'],
                        'user_nickname' => $to_user_info['user_nickname']
                    ];

                    $pics = [];
                    if ($pics_list = json_decode($v['pics'], true)) {
                        foreach ($pics_list as $values) {
                            $pics[] = PicUrl::Question($values, $this->getDi());
                        }
                    }
                    $item[$key]['pics'] = $pics;
                    $item[$key]['content'] = unserialize(base64_decode($v['content'])) ?: $v['content'];
                    unset($item[$key]['user_id'], $item[$key]['to_user_id']);
                }
                $expertReplyList[$k]['item'] = $item;
            } else {
                $expertReplyList[$k]['item'] = [];
            }
        }

        $this->setResult($expertReplyList);
    }

}